﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using DataObjectsPhone;
using ee_wp_tourmaker.WebServices;
using System.Device.Location;
using Agent;
using Microsoft.Phone.Controls.Maps.Platform;
using Microsoft.Phone.Controls.Maps;

namespace ee_wp_tourmaker
{
    public partial class MapPage : PhoneApplicationPage
    {
        LocationServices locationServices = new LocationServices();
        public MapPage()
        {
            InitializeComponent();
        }

        private void mainMap_Loaded(object sender, RoutedEventArgs e)
        {
            //mainMap.NavigationVisibility = System.Windows.Visibility.Collapsed;

            locationServices = new LocationServices();
            locationServices.Start();
            foreach (Challenge challenge in App.ChallengeData.ActiveChallenges)
            {
                foreach (CheckPoint checkpoint in challenge.checkpointsList)
                {
                    Location l = new GeoCoordinate((double)checkpoint.Latitude, (double)checkpoint.Longitude);
                    Pushpin pin = new Pushpin();
                    if (checkpoint.Done)
                    {
                        pin.Background = new SolidColorBrush(Colors.Green);
                    }
                    else
                    {
                        pin.Background = new SolidColorBrush(Colors.Red);
                    }
                    pin.Location = l;
                    mainMap.Children.Add(pin);
                }
            }
            var currentPosition = locationServices.GeCurrentLocation();
            if (currentPosition.Location != GeoCoordinate.Unknown && currentPosition.Timestamp > DateTimeOffset.Now.Add(TimeSpan.FromMinutes(-5)))
            {
                mainMap.Children.Add(new Pushpin
                { 
                    Content = "I am here",
                    Location = new GeoCoordinate(
                    currentPosition.Location.Latitude,
                    currentPosition.Location.Longitude)
                });
                mainMap.SetView(new GeoCoordinate(currentPosition.Location.Latitude, currentPosition.Location.Longitude), 10);                
                locationServices.Stop();
            }
            else
            {
                locationServices.PositionChanged += new LocationServices.PositionChangedEventHandler(locationServices_PositionChanged);
            }
        }

        void locationServices_PositionChanged(object sender, LocationChangeEventArgs e)
        {
            var currentPosition = e.Location;
            mainMap.Children.Add(new Pushpin
            {
                Location = new GeoCoordinate(
                currentPosition.Location.Latitude,
                currentPosition.Location.Longitude)
            });
            mainMap.SetView(new GeoCoordinate(currentPosition.Location.Latitude, currentPosition.Location.Longitude), 10);                
                
            locationServices.Stop();
        }

        private void ButtonZoomIn_Click(object sender, RoutedEventArgs e)
        {
            mainMap.ZoomLevel += 1;
        }

        private void ButtonZoomOut_Click(object sender, RoutedEventArgs e)
        {
            mainMap.ZoomLevel -= 1;
        }

    }
}
